package com.example.materialviewpagerdemo;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.materialviewpagerdemo.Presenter.CurrentDataController;
import com.example.materialviewpagerdemo.bean.CurrentData;
import com.example.materialviewpagerdemo.bean.StaticData;
import com.example.materialviewpagerdemo.util.ChartUtil;
import com.example.materialviewpagerdemo.util.LoadingUtil;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.zyao89.view.zloading.ZLoadingDialog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ChartActivity extends AppCompatActivity {
    LoadingUtil loadingUtil;
    Typeface typeface;
    @BindView(R.id.lineChart)
    LineChart lineChart;
    List<Entry> list = new ArrayList<>();
    List<BarEntry> barlist = new ArrayList<>();

    ZLoadingDialog zLoadingDialog;
    @BindView(R.id.chart_pm_one)
    BarChart chartPmOne;


    @BindView(R.id.chart_name)
    TextView chartName;
    @BindView(R.id.chart_currentdata)
    TextView chartCurrentdata;
    @BindView(R.id.unit)
    TextView unit;
    @BindView(R.id.lineChart_name)
    TextView lineChartName;
    @BindView(R.id.barchart_name)
    TextView barchartName;
    @BindView(R.id.card_view_barchart)
    CardView cardViewBarchart;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chart);
        ButterKnife.bind(this);
        zLoadingDialog = new ZLoadingDialog(this);
        loadingUtil = new LoadingUtil(zLoadingDialog);
        loadingUtil.showDialog();
        setTextData();
        getDataList(7);
    }


    private void getDataList(int number) {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(StaticData.url)
                .build();
        CurrentDataController currentDataController = retrofit.create(CurrentDataController.class);
        Call<List<CurrentData>> call = currentDataController.query(number);
        call.enqueue(new Callback<List<CurrentData>>() {
            @Override
            public void onResponse(Call<List<CurrentData>> call, Response<List<CurrentData>> response) {
                if (response.body() != null) {
                    ListIterator<CurrentData> listIterator = null;
                    try {
                        listIterator = response.body().listIterator();
                    }catch (NullPointerException e){
                        return;
                    }
                    int i = 0;
                    while (listIterator.hasNext()) {

                        i++;
                        CurrentData currentData = listIterator.next();
                        addDataList(currentData,i);
                    }

                    Collections.reverse(list);
                    Collections.reverse(barlist);
                    ChartUtil.setChartData(lineChart, list);
                    ChartUtil.setBarChartData(chartPmOne, barlist);
                    loadingUtil.cancleDialog();
                }


            }

            @Override
            public void onFailure(Call<List<CurrentData>> call, Throwable t) {
                loadingUtil.cancleDialog();
                System.out.println("失败");
            }
        });
    }

    public void addDataList(CurrentData currentData,int i){
        Entry entry=null;
        BarEntry barEntry=null;
        i=8-i;
        switch ((int) getIntent().getExtras().get("id")) {
            case 0:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getFengsu()), ChartUtil.praseData(currentData.getDataTime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getFengsu()), ChartUtil.praseData(currentData.getDataTime()));
                break;
            case 1:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getFengxiang()), ChartUtil.praseData(currentData.getDataTime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getFengxiang()), ChartUtil.praseData(currentData.getDataTime()));

                break;
            case 2:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSunLight()), ChartUtil.praseData(currentData.getDataTime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSunLight()), ChartUtil.praseData(currentData.getDataTime()));

                break;
            case 3:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirTemp1()), ChartUtil.praseData(currentData.getDataTime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirTemp1()), ChartUtil.praseData(currentData.getDataTime()));

                break;
            case 4:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirHumi1()), ChartUtil.praseData(currentData.getDataTime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirHumi1()), ChartUtil.praseData(currentData.getDataTime()));

                break;
            case 5:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilHumi1()), ChartUtil.praseData(currentData.getDataTime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilHumi1()), ChartUtil.praseData(currentData.getDataTime()));

                break;
            case 6:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilSalt1()), ChartUtil.praseData(currentData.getDataTime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilSalt1()), ChartUtil.praseData(currentData.getDataTime()));

                break;
            case 7:

                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilTemp1()), ChartUtil.praseData(currentData.getDataTime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilTemp1()), ChartUtil.praseData(currentData.getDataTime()));

                break;
            case 8:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirTemp2()), ChartUtil.praseData(currentData.getDataTime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirTemp2()), ChartUtil.praseData(currentData.getDataTime()));

                break;
            case 9:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirHumi2()), ChartUtil.praseData(currentData.getDataTime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirHumi2()), ChartUtil.praseData(currentData.getDataTime()));

                break;
            case 10:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilHumi2()), ChartUtil.praseData(currentData.getDataTime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilHumi2()), ChartUtil.praseData(currentData.getDataTime()));

                break;
            case 11:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilSalt2()), ChartUtil.praseData(currentData.getDataTime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilSalt2()), ChartUtil.praseData(currentData.getDataTime()));

                break;
            case 12:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilTemp2()), ChartUtil.praseData(currentData.getDataTime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilTemp2()), ChartUtil.praseData(currentData.getDataTime()));

                break;
        }
        list.add(entry);
        barlist.add(barEntry);

    }




    public void setTextData() {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(StaticData.url)
                .build();
        CurrentDataController currentDataController = retrofit.create(CurrentDataController.class);
        Call<CurrentData> call = currentDataController.queryOne();
        call.enqueue(new Callback<CurrentData>() {
            @Override
            public void onResponse(Call<CurrentData> call, Response<CurrentData> response) {
                try {
                    setData(response);
                }catch (NullPointerException e){
                    Toast.makeText(ChartActivity.this,"数据获取失败,请检查网络状态",Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(Call<CurrentData> call, Throwable t) {

            }
        });


    }


    public void setData(Response<CurrentData> response) {
        switch ((int) getIntent().getExtras().get("id")) {
            case 0:
                unit.setText("m/s");
                setRepeatName("风速");
                chartCurrentdata.setText(response.body().getFengsu());
                break;
            case 1:
                unit.setText("°");
                setRepeatName("风向");
                chartCurrentdata.setText(response.body().getFengxiang());
                break;
            case 2:
                unit.setText("Lx");
                setRepeatName("光照强度");
                chartCurrentdata.setText(response.body().getSunLight());
                break;
            case 3:
                unit.setText("℃");
                setRepeatName("空气温度1");
                chartCurrentdata.setText(response.body().getAirTemp1());
                break;
            case 4:
                unit.setText("RH%");
                setRepeatName("空气湿度1");
                chartCurrentdata.setText(response.body().getAirHumi1());
                break;
            case 5:
                unit.setText("RH%");
                setRepeatName("土壤湿度1");
                chartCurrentdata.setText(response.body().getSoilHumi1());
                break;
            case 6:
                unit.setText("‰");
                setRepeatName("土壤盐分1");

                chartCurrentdata.setText(response.body().getSoilSalt1());
                break;
            case 7:
                unit.setText("℃");
                setRepeatName("土壤温度1");
                chartCurrentdata.setText(response.body().getSoilTemp1());
                break;
            case 8:
                unit.setText("℃");
                setRepeatName("空气温度2");
                chartCurrentdata.setText(response.body().getAirTemp2());
                break;
            case 9:
                unit.setText("RH%");
                setRepeatName("空气湿度2");
                chartCurrentdata.setText(response.body().getAirHumi2());
                break;
            case 10:
                unit.setText("RH%");
                setRepeatName("土壤湿度2");
                chartCurrentdata.setText(response.body().getSoilHumi2());
                break;
            case 11:
                unit.setText("‰");
                setRepeatName("土壤盐分2");
                chartCurrentdata.setText(response.body().getSoilSalt2());
                break;
            case 12:
                unit.setText("℃");
                setRepeatName("土壤温度2");
                chartCurrentdata.setText(response.body().getSoilTemp2());
                break;

        }


    }
    public void setRepeatName (String name){
        chartName.setText("实时" + name);
        lineChartName.setText(name + "-折线图");
        barchartName.setText(name + "-柱状图");
    }
}
